package com.hongkzh.www.adv.controller;

import com.auth0.jwt.interfaces.Claim;
import com.hongkzh.www.adv.service.CarouselService;
import com.hongkzh.www.adv.service.CityService;
import com.hongkzh.www.adv.service.EnterpriseAdvService;
import com.hongkzh.www.conmon.*;
import com.hongkzh.www.swagger.annotation.*;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Kv;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

import java.util.List;
import java.util.Map;

/**
 * @author 罗天文
 */
@Api(tag = "cityAdv", description = "城市广告")
public class CityAdvController extends BaseController {
    /**
     * 上传文件基础虚拟路径
     */
    public static final String USERFILES_BASE_URL = "/userfiles/1/images/cityAdv/";
    CityService cityService = new CityService();
    static CarouselService carouselService = new CarouselService();

    @ApiOperation(url = "/v1/cityAdv/getMain", tag = "cityAdv", httpMethod = "post", description = "获取城市广告主页信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "{ carousels[imgSrc 图片地址 ,imgLinks 图片链接 imgRank 图片排序 ] " +
                            "hots[id 城市id ,cityName 城市名称 subtitle副标题 imgSrc图片地址 ]}"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void getMain() {
        ReqResponse<Record> r = new ReqResponse();
        Record r2 = new Record();
        List<Record> record = carouselService.getCityCarouselList();
        r2.set("carousels", null);
        if (null != record) {
            r2.set("carousels", record);
        }

        List<Record> hots = cityService.getHots();
        r2.set("hots", null);
        if (null != hots) {
            r2.set("hots", hots);
        }
        r.setData(r2);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/cityAdv/getAllHots", tag = "cityAdv", httpMethod = "post", description = "获取城市广告热门城市列表")
    @Params({
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description =
                            "hots[cityId 城市id ,cityName 城市名称 subtitle副标题 imgSrc图片地址 ]}"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void getAllHots() {

        int pageNumber = getParaToInt("pageNumber");
        ReqResponse<Page<Record>> r = new ReqResponse();
        Page<Record> record = cityService.getAllHots(pageNumber);
        r.setData(record);
        renderJson(r);


    }


    @ApiOperation(url = "/v1/cityAdv/getCityAdvList", tag = "cityAdv", httpMethod = "post", description = "获取城市广告人气分页信息")
    @Params({
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[{ isweb  1是h5 0不是  cityAdvId 游记id coverImgSrc 图片地址   title 标题    praiseCount 点赞数 rewardCount 奖励数  cityName 城市名称 advType 1数据 2 广告 vedioLength 视频长度 type 类型  1自媒体 2企业广告3城市广告4传统文化 totalMoney 大于0 表示有红包 }]"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void getCityAdvList() {

        int pageNumber = getParaToInt("pageNumber");
        ReqResponse<Page<Record>> r = new ReqResponse();
        Page<Record> record = cityService.getCityAdvList(pageNumber);
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/cityAdv/getCityNearbyAdvList", tag = "cityAdv", httpMethod = "post", description = "获取附近分页信息")
    @Params({

            @Param(name = "latitude", description = "纬度", required = true, dataType = "string"),
            @Param(name = "longitude", description = "经度", required = true, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[{isweb  1是h5 0不是  cityAdvId 游记id coverImgSrc 图片地址   title 标题   praiseCount 点赞数 rewardCount 奖励数    distance 距离  advType 1数据 2 广告 vedioLength 视频长度 type 类型  1自媒体 2企业广告3城市广告4传统文化}]"),

                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void getCityNearbyAdvList() {
        double latitude = Double.parseDouble(getPara("latitude"));
        double longitude = Double.parseDouble(getPara("longitude"));
        int pageNumber = getParaToInt("pageNumber");
        ReqResponse<Page<Record>> r = new ReqResponse();

        Page<Record> record = cityService.getCityNearbyAdvList(latitude, longitude, pageNumber);
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/cityAdv/getCityInfoById", tag = "cityAdv", httpMethod = "post", description = "获取城市广告详细信息")
    @Params({

            @Param(name = "cityId", description = "城市id", required = true, dataType = "string"),
            @Param(name = "freginCity", description = "城市名称", required = true, dataType = "string")

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "{ cityId 城市id " +
                            "imgSrc 图片地址 , officialVideo 官方视频 officialCoverImgSrc 官方视频封面  traditionalVideo 传统文化  traditionalCoverImgSrc 传统文化封面" +
                            "categorys[categoryId 分类id ,categoryName 分类名称 cityId 城市id]}"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void getCityInfoById() throws Exception {
        String cityId = getPara("cityId");
        String freginCity = getPara("freginCity");
        if (!"-999".equals(cityId)){
            freginCity = null;
        }

        ReqResponse<Record> r = new ReqResponse();
        Record r2 = cityService.getCityInfoById(cityId, freginCity);
        r.setData(r2);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/cityAdv/getCategoryCityAdvList", tag = "cityAdv", httpMethod = "post", description = "获取城市广告分类列表")
    @Params({
            @Param(name = "cityId", description = "城市id", required = true, dataType = "string"),
            @Param(name = "categoryId", description = "分类id", required = false, dataType = "string"),
            @Param(name = "pageNumber", description = "页码", required = true, dataType = "string"),
            @Param(name = "freginCity", description = "国外城市名称", required = true, dataType = "freginCity")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[{   cityAdvId 游记id coverImgSrc 图片地址   title 标题    praiseCount 点赞数 rewardCount 奖励数  cityName 城市名称 advType 1数据 2 广告 vedioLength 视频长度 type 类型  1自媒体 2企业广告3城市广告4传统文化 advType 1数据 2 广告  }]"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void getCategoryCityAdvList() {
        String cityId = getPara("cityId");
        String categoryId = getPara("categoryId");
        int pageNumber = getParaToInt("pageNumber");
        String freginCity = getPara("freginCity");
        ReqResponse<Page<Record>> r = new ReqResponse();
        Page<Record> record = cityService.getCategoryCityAdvList(pageNumber, cityId, categoryId, freginCity);
        r.setData(record);
        renderJson(r);
    }


    @ApiOperation(url = "/v1/cityAdv/getCityAdvInfo", tag = "cityAdv", httpMethod = "post", description = "获取城市游记详情")
    @Params({
            @Param(name = "cityAdvId", description = "城市广告id", required = true, dataType = "string"),
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = " cityAdvId 游记id title 标题 note 内容" +
                            "headImg 用户头像 uid 用户id name 用户名称 sex 性别  " +
                            "collectionCount 收藏总数commentCount评论总数 playCount查看总数 rewardCount 奖励总数 praiseCount 点赞总数" +
                            "createDate 发布日期 collectionState 1收藏0未收藏 praiseState 1点赞0未点赞 cityName 城市名称" +
                            "products[productId 商品id title 标题 imgSrc图片 price 价格 num 数量]" +
                            "imgs[imgSrc图片]"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    @Clear
    public void getCityAdvInfo() {
        String cityAdvId = getPara("cityAdvId");
        String token = getPara("token");

        String loginUid = "";
        if (StrKit.notBlank(token)) {
            Map<String, Claim> map = JwtUtil.unsign(token);
            if (map != null) {
                loginUid = map.get("user_id").asString();
            }
        }
        ReqResponse<Record> r = new ReqResponse();
        Record record = cityService.getCityAdvInfo(cityAdvId, loginUid);
        r.setData(record);
        renderJson(r);
    }

    @ApiOperation(url = "/v1/cityAdv/getCityAdvMediaInfo", tag = "cityAdv", httpMethod = "post", description = "获取城市视频详情")
    @Params({
            @Param(name = "cityAdvId", description = "城市广告id", required = true, dataType = "string"),
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "  {  cityAdvId 游记id    note 内容 isRed 1是有红包 0没有" +
                            " playCount 播放总数 rewardCount 奖励总数 praiseCount 点赞总数 commentCount 评论数  collectionCount 收藏总数 " +
                            "collectionState 已经收藏 1   没收藏 0 praiseState 已经点赞 1  没点赞 0  " +
                            "cityName 城市名称  " +
                            "uid 用户id name  用户名称 headImg 用户头像 sex性别  " +
                            "coverImgSrc 图片地址   imgSrc 视频地址 ,  title 标题    " +
                            "createDate 创建时间  " +
                            "tags[width 屏幕宽度 high  屏幕高度 tagId 标签id  type 标签类型 1圈子 2用户 3自定义  tagName 标签名称 x 标签x轴数  y 标签y轴数 orientation 标签方向0 左,1 右 ]" +
                            "products[productId 商品id title 标题 imgSrc图片 price 价格 num 数量]" +
                            "nextMediaAdvId 下一个视频id" + "historyId 为空 用户未领取过红包 不为空 用户已领取"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    @Clear
    public void getCityAdvMediaInfo() {
        String cityAdvId = getPara("cityAdvId");
        String token = getPara("token");

        String loginUid = "";
        if (StrKit.notBlank(token)) {
            Map<String, Claim> map = JwtUtil.unsign(token);
            if (map != null) {
                loginUid = map.get("user_id").asString();
            }
        }
        ReqResponse<Record> r = new ReqResponse();
        try {
            Record record = cityService.getCityAdvMediaInfo(cityAdvId, loginUid);
            r.setData(record);
            renderJson(r);
        } catch (BusinessException e) {
            r.setCode(1);
            r.setMsg(e.getErrMsg());
            renderJson(r);
        }

    }

    @ApiOperation(url = "/v1/cityAdv/collection", tag = "cityAdv", httpMethod = "post", description = "通过用户id  state收藏状态 1收藏0取消收藏")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "state", description = "收藏状态 1收藏0取消收藏", required = true, dataType = "string"),
            @Param(name = "cityAdvId", description = "城市广告id", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "data", description = " 总收藏数"),
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })

    public void collection() {
        EnterpriseAdvService enterpriseAdvService2 = enhance(EnterpriseAdvService.class);
        String loginUid = getAttr("loginUid");
        String id = getPara("cityAdvId");
        String state = getPara("state");
        ReqResponse<String> r = new ReqResponse();
        try {
            r.setData(enterpriseAdvService2.collection(loginUid, id, state));

        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(1);
            r.setMsg("操作失败");
        }
        renderJson(r);
    }


    @ApiOperation(url = "/v1/cityAdv/praise", tag = "cityAdv", httpMethod = "post", description = "通过用户id  state点赞状态 1点赞0取消点赞")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "state", description = "点赞状态 1点赞0取消点赞", required = true, dataType = "string"),
            @Param(name = "cityAdvId", description = "城市广告id", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "data", description = " 总点赞数"),
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })

    public void praise() {
        EnterpriseAdvService enterpriseAdvService2 = enhance(EnterpriseAdvService.class);
        String loginUid = getAttr("loginUid");
        String id = getPara("cityAdvId");
        String state = getPara("state");
        ReqResponse<String> r = new ReqResponse();
        try {
            r.setData(enterpriseAdvService2.praise(loginUid, id, state));

        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(1);
            r.setMsg("操作失败");
        }
        renderJson(r);
    }

    @ApiOperation(url = "/v1/cityAdv/reward", tag = "cityAdv", httpMethod = "post", description = "城市打赏")
    @Params({
            @Param(name = "loginUid", description = "当前登录用户id", required = true, dataType = "string"),

            @Param(name = "cityAdvId", description = "城市广告id", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "{    }"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void reward() throws Exception {
        String loginUid = getAttr("loginUid");
        String id = getPara("cityAdvId");
        Integer money = 1;
        ReqResponse<String> r = new ReqResponse();
        try {
            r.setData(cityService.reward(loginUid, id, money));
        } catch (Exception e) {
            r.setCode(1);
            r.setMsg("乐币不够");
        }

        renderJson(r);
    }

    /* @ApiOperation(url = "/v1/cityAdv/getInfoMediaAdvCommentList", tag = "cityAdv", httpMethod = "post",description = "获取城市广告评论列表")
     @Params({
             @Param(name = "cityAdvId", description = "城市广告id", required = true, dataType = "string"),
             @Param(name = "loginUid", description = "当前登录用户id", required = true, dataType = "string"),
             @Param(name = "pageNumber", description = "页码", required = true, dataType = "string")
     })
     @ApiResponses({
             @ApiResponse(code=200,message="成功",responseHeaders={
                     @ResponseHeader(name="code",description = " 0成功 1失败"),
                     @ResponseHeader(name="data",description = "  totalRow 总行数  pageNumber第几页 firstPage 是否第一页 lastPage 是否最后一页 totalPage 总页数 pageSize每页条数  list[{" +
                             "commentId 评论id userId 评论用户 uName 评论用户名称" +
                             " headImg 评论用户头像 content 内容 praiseState 1点赞0未点赞 " +
                             "  commentCount 回复总数 praiseCount 点赞数 createDate 创建时间 }] "),

                     @ResponseHeader(name="msg",description = "失败原因") })

     })
     public void getInfoMediaAdvCommentList(){
         String id=getPara("cityAdvId");
         String loginUid=getAttr("loginUid");
         int pageNumber=getParaToInt("pageNumber");
         ReqResponse<Page<Record>> r=new ReqResponse();
         AdvService advService=new AdvService();
         Page<Record> record= advService.getAdvCommentsById(id,pageNumber,loginUid);
         r.setData(record);
         renderJson(r);
     }*/
    @ApiOperation(url = "/v1/cityAdv/saveCityAdv", tag = "cityAdv", httpMethod = "post", description = "写游记")
    @Params({
            @Param(name = "cityAdvId", description = "游记id", required = false, dataType = "string"),
            @Param(name = "coverImgSrc", description = "封面", required = true, dataType = "string"),
            @Param(name = "coverImgWidth", description = "封面图片宽", required = true, dataType = "string"),
            @Param(name = "coverImgHeight", description = "封面图片高", required = true, dataType = "string"),
            @Param(name = "title", description = "标题", required = true, dataType = "string"),
            @Param(name = "categoryId", description = "分类id", required = true, dataType = "string"),
            @Param(name = "cityId", description = "市id", required = true, dataType = "string"),
            @Param(name = "content", description = "内容", required = true, dataType = "string"),
            @Param(name = "products", description = "商品数组", required = false, dataType = "string"),
            @Param(name = "isShowArea", description = "1显示地区0否", required = true, dataType = "string"),
            @Param(name = "loginUid", description = "当前登录用户id", required = true, dataType = "string"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = ""),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })

    public void saveCityAdv() {
        UploadFile coverImgSrc = getFile("coverImgSrc");
        String coverImgSrcImg = "";
        getResponse().addHeader("Access-Control-Allow-Origin", "*");
        try {
            if (null != coverImgSrcImg) {
                coverImgSrcImg = USERFILES_BASE_URL + DateUtil.FormatDateMMDD() + "/";
                coverImgSrcImg = getFilePath(coverImgSrcImg, coverImgSrc);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
        String title = getPara("title");
        String content = getPara("content");
        String[] products = getParaValues("products");
        String isShowArea = getPara("isShowArea");
        String loginUid = getAttr("loginUid");
        String categoryId = getPara("categoryId");
        String cityId = getPara("cityId");
        String cityAdvId = getPara("cityAdvId");
        String coverImgWidth = getPara("coverImgWidth");
        String coverImgHeight = getPara("coverImgHeight");

        ReqResponse<String> r = new ReqResponse();
        CityService cityService2 = enhance(CityService.class);
        cityService2.saveCityAdv(title, content, coverImgSrcImg, products, isShowArea, loginUid,
                categoryId, cityId, cityAdvId, coverImgWidth, coverImgHeight);

        renderJson(r);
    }

    @ApiOperation(url = "/v1/cityAdv/saveCityAdvPhoto", tag = "cityAdv", httpMethod = "post", description = " 上传游记图片")
    @Params({
            @Param(name = "loginUid", description = "当前用户id", required = true, dataType = "string"),
            @Param(name = "imgSrc", description = "图片文件", required = true, dataType = "string")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", responseHeaders = {
                    @ResponseHeader(name = "code", description = " 0成功 1失败"),
                    @ResponseHeader(name = "data", description = "文件路径"),
                    @ResponseHeader(name = "msg", description = "失败原因")})

    })
    public void saveCityAdvPhoto() {
        UploadFile uf = getFile("imgSrc");
        getResponse().addHeader("Access-Control-Allow-Origin", "*");


        ReqResponse<String> r = new ReqResponse();

        try {
            String coverImgSrc = USERFILES_BASE_URL + DateUtil.FormatDateMMDD() + "/";
            coverImgSrc = getFilePath(coverImgSrc, uf);
            //FileUtils.copyFileUsingFileChannels(uf.getFile(), file);
            r.setData(PropKit.get("fileServer") + coverImgSrc);
        } catch (Exception e) {
            e.printStackTrace();
            r.setCode(1);
        }


        renderJson(r);
    }
/*

    public void test(){
        List<UploadFile> list =getFiles();
        for (UploadFile u:list
             ) {
            System.out.println(u.getUploadPath());
        }
        System.out.println(getParaMap());
        ReqResponse<Record> r=new ReqResponse();
        renderJson(r);
    }
*/


}
